<?php

namespace App\Models;

use App\Services\Base\HelpService;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;

/**
 * This is the model class for table "user_auth"
 *
 * @property string $id
 * @property string $user_id 用户ID
 * @property string $country_id
 * @property string $country_code 国家代码
 * @property string $realname 真实姓名
 * @property string $id_card 证件号码
 * @property string $birthday 出生日期
 * @property string $address 居住地址
 * @property string $city 城市
 * @property string $postal_code 邮政编码
 * @property string $extra 额外信息
 * @property string $phone 电话号码
 * @property string $type 证件类型 默认1身份证
 * @property string $front_img 正面照
 * @property string $back_img 背面照
 * @property string $hand_img 手持照
 * @property string $check_time 审核时间
 * @property string $primary_status 初级认证状态 1已认证
 * @property string $status 高级认证状态 0未认证,1待审核,2已通过,3已驳回
 * @property string $remark
 * @property string $created_at
 * @property string $updated_at
 */
class UserAuth extends BaseModel
{
    //用户认证
    use HasDateTimeFormatter;

    protected $primaryKey = 'id';
    protected $table = 'user_auth';
    protected $guarded = [];

    protected $appends = ['primary_status_text', 'status_text'];

    protected $attributes = [
        'status' => 0,
    ];

    const STATUS_UNAUTH = 0;
    const STATUS_WAIT = 1;
    const STATUS_AUTH = 2;
    const STATUS_REJECT = 3;

    public static $statusMap = [
        self::STATUS_UNAUTH => '高级未认证',
        self::STATUS_WAIT => '待审核',
        self::STATUS_AUTH => '已通过',
        self::STATUS_REJECT => '已驳回',
    ];

    public static $primaryStatusMap = [
        0 => '初级未认证',
        1 => '初级认证通过',
    ];

    public function getStatusTextAttribute()
    {
        return self::$statusMap[$this->status];
    }

    public function getPrimaryStatusTextAttribute()
    {
        return self::$primaryStatusMap[$this->primary_status];
    }
    

    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'id');
    }

    public function index($params)
    {
        $data = ['verified' => false, 'status' => 0];
        /**
         * @var $user_auth UserAuth
         */
        $user_auth = UserAuth::query()->where('uid', $params['uid'])->first();
        if ($user_auth) {
            if ($user_auth->status == USER_AUTH_SUCCESS) {
                $data["verified"] = true;
            }
            $data["realname"] = $user_auth->realname;
            $data["id_card"] = $user_auth->id_card;
            $data["status"] = $user_auth->status;
        }

        return $data;
    }

    public function request($params)
    {
        try {
            /**
             * @var $user_auth UserAuth
             */
            $user_auth = UserAuth::query()->where('uid', $params['uid'])->first();
            if ($user_auth) {
                if ($user_auth->status == USER_AUTH_SUCCESS) {
                    return true;
                }
                $user_auth->update([
                    "realname" => $params['realname'],
                    "id_card" => $params['id_card'],
                    "front_img" => $params['front_img'],
                    "back_img" => $params['back_img']
                ]);
            } else {
                UserAuth::query()->create([
                    "uid" => $params['uid'],
                    "realname" => $params['realname'],
                    "id_card" => $params['id_card'],
                    "front_img" => $params['front_img'],
                    "back_img" => $params['back_img'],
                    "status" => USER_AUTH_CHECK
                ]);
            }
        } catch (\Exception $e) {
            HelpService::forceLog($e);
            return false;
        }

        return true;
    }


    public static function isVerify($params)
    {
        $verified = false;
        /**
         * @var $user_auth UserAuth
         */
        $user_auth = UserAuth::query()->where('uid', $params['uid'])->first();
        $user_auth && $verified = $user_auth->status == USER_AUTH_SUCCESS;
        return $verified;
    }


}
